import { createSlice } from "@reduxjs/toolkit";
const doctorSlice = createSlice({
    name:'doctorSlice',
    initialState:{
       list:JSON.parse(localStorage.getItem('doctor')||'[]')
    },
    reducers:{
        //添加
        add(state,{payload}){
            const data=state.list.find((item:any)=>item.id==payload.id)
            if(data){
               //删除
               state.list=state.list.filter((item:any)=>item.id!=payload.id)
            }else{
                state.list.unshift(payload)
            }
          localStorage.setItem('doctor',JSON.stringify(state.list))
        },
        //删除
        remove(state,{payload}){
            state.list=state.list.filter((item:any)=>item.id!=payload.id)
            localStorage.setItem('doctor',JSON.stringify(state.list))
        }
    }
})
//抛出actions
export const {add,remove} = doctorSlice.actions
//抛出reducer
export default doctorSlice.reducer